// pages/around/around.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iconList: [{
      icon: 'shop',
      color: 'red',
      badge: 0,
      name: '餐饮',
      amap: '050000'
    }, {
      icon: 'recordfill',
      color: 'orange',
      badge: 0,
      name: '风景',
      amap: '110000'
    }, {
      icon: 'read',
      color: 'yellow',
      badge: 0,
      name: '科教',
      amap:'140000'
    }, {
      icon: 'service',
      color: 'olive',
      badge: 0,
      name: '生活服务',
      amap:'070000'
    }, {
      icon: 'safe',
      color: 'cyan',
      badge: 0,
      name: '医疗',
      amap:'090000'
    }, {
      icon: 'goodsfill',
      color: 'blue',
      badge: 0,
      name: '购物',
      amap:'060000'
    }, {
      icon: 'discoverfill',
      color: 'purple',
      badge: 0,
      name: '发现'
    }, {
      icon: 'questionfill',
      color: 'mauve',
      badge: 0,
      name: '帮助'
    }, {
      icon: 'commandfill',
      color: 'purple',
      badge: 0,
      name: '问答'
    }, {
      icon: 'brandfill',
      color: 'mauve',
      badge: 0,
      name: '版权'
    }],
    gridCol: 3,
    geo_address:'',
    location:'',
    inputValue:'',
    around_data:''
  },


  // 事件函数:跳转事件
  nav_detail: function (e) {
    console.log("这是around页面的点击事件数据：", e.currentTarget.dataset)
    var title = e.currentTarget.dataset.name
    var amap = e.currentTarget.dataset.amap
    var now_location = this.data.location
    wx.navigateTo({
      // 'path?key=value&key2=value2'
      url: '/pages/around_detail/around_detail' + '?' + 'detail_title=' + title + '&' + 'detail_location='+now_location + '&' +'detail_amap='+amap,
    })
  },

  // 用户输入函数:输入事件
  bindKeyInput: function (e) {
    console.log("用户输入：",e.detail.value)
    this.setData({
      inputValue: e.detail.value
    })

  },
  // 搜索事件：点击事件
  search: function (e){
    var that = this
    console.log("点击搜索产生的数据：",e.currentTarget.dataset.inputv)
    var search_value = e.currentTarget.dataset.inputv
    wx.request({
      url: 'https://restapi.amap.com/v3/place/around?parameters ', //仅为示例，并非真实的接口地址
      data: {
        key: 'fadcb9827e14d0d87480767c7e77aede',
        location: this.data.location,
        keywords: search_value
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
        that.setData({
          around_data:res.data
        })
      }
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that=this
    // 1. 获取当前位置--经纬度
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        console.log("获取当前位置--经纬度:",res)
        that.setData({
          location:longitude+','+latitude
        })
        // 先位置，在逆地理编码
        // 2. 高德API获取结构化地址
        wx.request({
          url: 'https://restapi.amap.com/v3/geocode/regeo?parameters', //仅为示例，并非真实的接口地址
          data: {
            key: 'fadcb9827e14d0d87480767c7e77aede',
            location: longitude+','+latitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            console.log("高德API获取结构化地址:",res.data)
            var component = res.data.regeocode.addressComponent
            that.setData({
              geo_address:component.city+' '+component.district+' '+component.township
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})